package com.cashmate.struts;

import com.cashmate.hibernate.dao.BusinessDAO;
import com.cashmate.hibernate.pojo.Businesses;
import com.cashmate.hibernate.pojo.Employees;
import com.cashmate.hibernate.pojo.Usertypes;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.ServletActionContext;

public class EditBusiness extends ActionSupport {

    private static Logger logger = Logger.getLogger(EditBusiness.class);
    //This properties have setters below... The setters are called and populated by the grid.
    private String id;//the column in grid which we set key=true
    private String businessname;
    private String businesstype;
    private String dailyaveragesales;
    private String streetaddress;
    private String barangay;
    private String cityormunicipality;
    private String province;
    private String postalcode;
    private String floorarea;
    private String lotarea;
    private String stability;
    private String model;
    private String make;
    private String land;
    private String monthlyrental;
    private String landlordname;
    private String landlordaddress;
    private String landlordphoneno;
    private BusinessDAO bdao = (BusinessDAO) ServletActionContext.getServletContext().getAttribute("businessDAO");
    private Employees emps = (Employees) ServletActionContext.getRequest().getSession().getAttribute("employee");
    private Usertypes ut = (Usertypes) ServletActionContext.getRequest().getSession().getAttribute("usertype");

    //Notice we don't have a success result because we do not need to redirect the user when he/she updates the dbase
    //We redirect him/her only if an error occurred.
    @Action(value = "/edit-business", results = {
        @Result(name = "error", type = "redirectAction", location = "../error/messages.jsp")
    })
    @Override
    public String execute() throws Exception {
        Businesses b = bdao.get(Integer.parseInt(id));
        try {
            String fieldname = "";
            String fieldvalue = "";
            if (businessname != null) {
                b.setBusinessname(businessname);
                fieldname = "businessname";
                fieldvalue = businessname;
            }
            if (businesstype != null) {
                b.setBusinesstype(businesstype);
                fieldname = "businesstype";
                fieldvalue = businesstype;
            }
            if (dailyaveragesales != null) {
                b.setDailyaveragesales(Integer.parseInt(dailyaveragesales));
                fieldname = "dailyaveragesales";
                fieldvalue = dailyaveragesales;
            }
            if (streetaddress != null) {
                b.setStreetaddress(streetaddress);
                fieldname = "streetaddress";
                fieldvalue = streetaddress;
            }
            if (barangay != null) {
                b.setBarangay(barangay);
                fieldname = "barangay";
                fieldvalue = barangay;
            }
            if (cityormunicipality != null) {
                b.setCityormunicipality(cityormunicipality);
                fieldname = "cityormunicipality";
                fieldvalue = cityormunicipality;
            }
            if (province != null) {
                b.setProvince(province);
                fieldname = "province";
                fieldvalue = province;
            }
            if (postalcode != null) {
                b.setPostalcode(postalcode);
                fieldname = "postalcode";
                fieldvalue = postalcode;
            }
            if (floorarea != null) {
                b.setFloorarea(floorarea);
                fieldname = "floorarea";
                fieldvalue = floorarea;
            }
            if (lotarea != null) {
                b.setLotarea(lotarea);
                fieldname = "lotarea";
                fieldvalue = lotarea;
            }
            if (stability != null) {
                b.setStability(stability);
                fieldname = "stability";
                fieldvalue = stability;
            }
            if (model != null) {
                b.setModel(model);
                fieldname = "model";
                fieldvalue = model;
            }
            if (make != null) {
                b.setMake(make);
                fieldname = "make";
                fieldvalue = make;
            }
            if (land != null) {
                b.setLand(land);
                fieldname = "land";
                fieldvalue = land;
            }
            if (monthlyrental != null) {
                b.setMonthlyrental(Integer.parseInt(monthlyrental));
                fieldname = "monthlyrental";
                fieldvalue = monthlyrental;
            }
            if (landlordname != null) {
                b.setLandlordname(landlordname);
                fieldname = "landlordname";
                fieldvalue = landlordname;
            }
            if (landlordaddress != null) {
                b.setLandlordaddress(landlordaddress);
                fieldname = "landlordaddress";
                fieldvalue = landlordaddress;
            }
            if (landlordphoneno != null) {
                b.setLandlordphoneno(landlordphoneno);
                fieldname = "landlordphoneno";
                fieldvalue = landlordphoneno;
            }
            bdao.update(b);
            logger.info(ut.getType() + " (" + emps.getIdnumber() + ") updated Businesses(" + b.getBusinessid() + ") with value: " + fieldname + ":" + fieldvalue);
        } catch (Exception e) {
            if (emps != null) {
                logger.fatal("(" + emps.getIdnumber() + "):" + e.getLocalizedMessage(), e);
            } else {
                logger.fatal(e.getLocalizedMessage(), e);
                e.printStackTrace();
            }
            addActionError(e.getLocalizedMessage());
            return ERROR;
        }
        return NONE;
    }
    //setters

    public void setBarangay(String barangay) {
        this.barangay = barangay;
    }

    public void setBusinessname(String businessname) {
        this.businessname = businessname;
    }

    public void setBusinesstype(String businesstype) {
        this.businesstype = businesstype;
    }

    public void setCityormunicipality(String cityormunicipality) {
        this.cityormunicipality = cityormunicipality;
    }

    public void setDailyaveragesales(String dailyaveragesales) {
        this.dailyaveragesales = dailyaveragesales;
    }

    public void setEmps(Employees emps) {
        this.emps = emps;
    }

    public void setFloorarea(String floorarea) {
        this.floorarea = floorarea;
    }

    public void setId(String id) {
        this.id = id;
    }

    public void setLand(String land) {
        this.land = land;
    }

    public void setLandlordaddress(String landlordaddress) {
        this.landlordaddress = landlordaddress;
    }

    public void setLandlordname(String landlordname) {
        this.landlordname = landlordname;
    }

    public void setLandlordphoneno(String landlordphoneno) {
        this.landlordphoneno = landlordphoneno;
    }

    public void setLotarea(String lotarea) {
        this.lotarea = lotarea;
    }

    public void setMake(String make) {
        this.make = make;
    }

    public void setModel(String model) {
        this.model = model;
    }

    public void setMonthlyrental(String monthlyrental) {
        this.monthlyrental = monthlyrental;
    }

    public void setPostalcode(String postalcode) {
        this.postalcode = postalcode;
    }

    public void setProvince(String province) {
        this.province = province;
    }

    public void setStability(String stability) {
        this.stability = stability;
    }

    public void setStreetaddress(String streetaddress) {
        this.streetaddress = streetaddress;
    }
}
